Skip to content

Add solution#506

Open
ProKesha wants to merge 3 commits into
mate-academy:masterfrom
ProKesha:develop
Open

Add solution#506
ProKesha wants to merge 3 commits into
mate-academy:masterfrom
ProKesha:develop

Conversation

@ProKesha
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job setting up the Category model and implementing all the required CRUD endpoints! The new parts of the application are well-structured. However, changes are needed because the new Category model isn't fully integrated with the Expense model, which is a core requirement of the task. Currently, the Expense model still stores the category as a simple string, which defeats the purpose of having a separate categories table.

To fix this, you'll need to establish a proper database relationship:

  1. In src/models/Expense.model.js, replace the category string field with a categoryId that will act as a foreign key.
  2. In src/models/models.js, define the association between Category and Expense (e.g., an expense belongsTo a category, and a category hasMany expenses).

Once you make these changes, the data model will be much more robust. You're very close to finishing this!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/models/Expense.model.js Outdated
Comment on lines +32 to +34
category: {
type: DataTypes.STRING,
allowNull: true,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that there's a dedicated Category model, storing the category as a string here is not ideal. A better approach is to use a foreign key relationship. Consider replacing this category field with a categoryId that references the id of a record in the categories table. You'll also need to define this association in src/models/models.js.

Comment thread src/models/models.js
Expense.belongsTo(User, {
foreignKey: 'userId',
as: 'user',
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've successfully set up the relationship between users and expenses. Now, you need to do the same for categories and expenses. A category can have many expenses, and an expense should belong to a single category. You should define those associations here.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Excellent work on this iteration! You've successfully addressed all the requirements by integrating the Category and Expense models correctly. I am approving your solution as it meets all the core functional requirements.

I do want to point out two things for you to consider for future improvements, though they are not blocking this approval:

  1. Linting Error: The automated check is failing because of a no-shadow linting error in src/createServer.js. This means a variable was declared in a nested scope with the same name as a variable in an outer scope. It's a good practice to rename one of them to avoid potential confusion.
  2. Security Best Practice: In src/db.js, you have a hardcoded fallback password. It's more secure to let the application fail to connect if the POSTGRES_PASSWORD environment variable is missing rather than using a default value.

Again, great job completing the task. Keep up the fantastic work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/db.js Outdated
dialect: 'postgres',
port: POSTGRES_PORT || 5432,
password: POSTGRES_PASSWORD || '123',
password: POSTGRES_PASSWORD || 'Mylifeuncut2015',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding credentials, even as a fallback, is a significant security risk. If the POSTGRES_PASSWORD environment variable is not set, this default password will be used, which could expose your database. It's much safer to let the application fail to connect if a password isn't provided through the environment variables, rather than falling back to a hardcoded value.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job on this task! You've successfully integrated PostgreSQL, implemented the category CRUD functionality, and addressed the previous feedback on security. I am approving your solution so you can continue progressing, but there is one point of inconsistency to consider for future improvements.

In src/createServer.js, the logic for handling a null category differs between creating and updating an expense. The POST /expenses endpoint incorrectly rejects a request with "category": null, while the PATCH /expenses/:expenseId endpoint correctly handles this as a way to unset the category. To ensure consistent API behavior, you should align the validation logic in the POST endpoint with the more robust logic used in the PATCH endpoint.

Overall, this is a very solid implementation. Keep up the excellent work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment thread src/createServer.js
Comment on lines +151 to +154
if (
(categoryId !== undefined || category !== undefined) &&
!resolvedCategoryId
) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a logical inconsistency in handling category assignment here compared to the PATCH endpoint. If a client sends "category": null in the request body to explicitly create an expense without a category, this condition will cause a 400 Bad Request with 'Category not found'.

The PATCH endpoint on line 275 correctly handles this by checking category !== null. You should apply similar logic here to ensure consistent behavior across your API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants